package cn.mumzone.pos.presenter;

import android.view.SearchEvent;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.SearchBillEvent;
import cn.mumzone.pos.model.BillDetailEntity;
import cn.mumzone.pos.model.BillInfoListBean;
import cn.mumzone.pos.model.source.BillsRepository;
import cn.mumzone.pos.presenter.contract.BillsItemContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created By Dlx
 * on 2019/6/22
 */
public class BillsItemPresenter extends AbsPresenter<BillsItemContract.View, BillsRepository> implements BillsItemContract.Presenter {
    private BillsRepository billRepo;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        billRepo = new BillsRepository();
    }

    @Override
    public void getBillDatas(String dateTime, int enterType, final String searchKey, final int pageNum) {
        ApiCallBack<PageListResponse<BillInfoListBean>> callBack = new ApiCallBack<PageListResponse<BillInfoListBean>>() {
            @Override
            public void onSuccess(PageListResponse<BillInfoListBean> listBean) {
                if ("0".equals(listBean.getPageTotal()) && pageNum == 1) {
                    if (StringUtils.isEmpty(searchKey)) {
                        mView.noSuchSearchResult();
                    } else {
                        mView.showError("");
                    }
                    return;
                }
                if (listBean.getList().size() < AppConstants.PAGE_SIZE) {
                    mView.onNoMore(true);
                } else {
                    mView.onNoMore(false);
                }
                if (StringUtils.isEmpty(searchKey)) {
                    mView.onDataReceived(listBean, pageNum == 1);
                } else {
                    mView.onSearchDataReceived(listBean, pageNum == 1);
                }
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                super.onNetworkError(response);
                mView.showNetError();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.getBillDatas(dateTime, searchKey, enterType + "", pageNum + "", AppConstants.PAGE_SIZE + "", callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void getGoodsBillDetailForPrint(String saleId, String orderType) {
        mView.showLoadingDialog();
        ApiCallBack<BillDetailEntity> callBack = new ApiCallBack<BillDetailEntity>() {
            @Override
            public void onSuccess(BillDetailEntity billDetailEntity) {
                mView.dismissLoadingDialog();
                mView.onPrintDataReceived(billDetailEntity);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        billRepo.getBillDetail(saleId, orderType, callBack);
        addSubscrebe(callBack);
    }

    public void initEvent() {
        Subscription subscription = RxBus.getDefault()
                .toObservable(SearchBillEvent.class)
                .subscribe(new Action1<SearchBillEvent>() {
                    @Override
                    public void call(SearchBillEvent searchBillEvent) {
                        mView.onSearchEvent(searchBillEvent.getSearchKey());
                    }
                });
        addSubscrebe(subscription);
    }
}
